package com.guang.service.impl;

import com.alibaba.excel.EasyExcel;
import com.guang.domain.UserInfo;
import com.guang.service.EasyExcelService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
@Slf4j
@AllArgsConstructor
public class EasyExcelServiceImpl implements EasyExcelService {


    private final ApplicationContext applicationContext;


    /**
     * excle文件导入实现
     * @param file
     */
    @Override
    public void easyExcelImport(MultipartFile file) {
        try {
            long beginTime = System.currentTimeMillis();
            //加载文件读取监听器
            EasyExcelImportHandler listener = applicationContext.getBean(EasyExcelImportHandler.class);
            //easyexcel的read方法进行数据读取
            EasyExcel.read(file.getInputStream(), UserInfo.class,listener).sheet().doRead();
            log.info("读取文件耗时：{}秒",(System.currentTimeMillis() - beginTime)/1000);
        } catch (IOException e) {
            log.error("导入异常", e.getMessage(), e);
        }
    }
}
